package com.isoft.controller.flight;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.bean.ResponseData;
import com.isoft.bean.ResultCode;
import com.isoft.controller.BaseController;
import com.isoft.entity.flight.FlightInfo;
import com.isoft.service.flight.FlightInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiuGaoMing
 * @version 1.0
 * @description:航班飞机信息页面
 * @date 2024/6/22 15:57
 */
@RestController
@RequestMapping("/flight")
@CrossOrigin
public class FlightInfoController extends BaseController {
    @Autowired
    private FlightInfoService flightInfoService;
//    查询所有信息
    @GetMapping("/all")
    public ResponseData<Object> selectAllFlightInfo(){
        List<FlightInfo> allFlightInfo = flightInfoService.findAllFlightInfo();
        return returnObject( allFlightInfo );
    }
//    根据CODE和CFTP进行模糊查询
   /* @GetMapping("/find")
    public ResponseData<Object> selectFlightInfoByCodeOrCFTP(String CODE,String CFTP){
        List<FlightInfo> flightInfoByCodeOrCftp = flightInfoService.findFlightInfoByCodeOrCftp( CODE, CFTP );
        return returnObject( flightInfoByCodeOrCftp );
    }*/
@GetMapping("/find")
public ResponseData<List<FlightInfo>> findFlightInfoByCodeOrCFTP(
        @RequestParam(value = "CODE", required = false) String CODE,
        @RequestParam(value = "CFTP", required = false) String CFTP,
        @RequestParam( defaultValue = "1") int currentPage,
        @RequestParam(defaultValue = "7") int pageSize) {

    // 使用MyBatis Plus的分页查询
    Page<FlightInfo> page = new Page<>(currentPage, pageSize);
    QueryWrapper<FlightInfo> queryWrapper = new QueryWrapper<>();
//    倒序
    queryWrapper.orderByDesc("ID");
    if (CODE != null && !CODE.isEmpty()) {
        queryWrapper.like("CODE", CODE);
    }
    if (CFTP != null && !CFTP.isEmpty()) {
        queryWrapper.like("CFTP", CFTP);
    }
    flightInfoService.page(page, queryWrapper);

    return ResponseData.success(page.getRecords(), page.getTotal());
}
//    分页查询
    @GetMapping("/flightInfos/page")
    public ResponseData<Object> getFlightInfosPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "5") int pageSize) {
        try {
            List<FlightInfo> flightInfos = flightInfoService.findFlightInfoByPage(currentPage, pageSize);
            long totalElements = flightInfoService.selectFlightInfoCount();
            return ResponseData.success(flightInfos, totalElements);
        } catch (Exception e) {
            // 处理异常，返回错误信息
            return ResponseData.failure(String.valueOf(ResultCode.COMMON_FAIL.getCode()), "获取数据失败：" + e.getMessage());
        }
    }

    //    删除
    @DeleteMapping("/{ID}")
    public ResponseData<Boolean> deleteFlightInfo(@PathVariable Integer ID){
        Boolean deleteFlightInfo = flightInfoService.deleteFlightInfo( ID );
        return returnBoolean( deleteFlightInfo );
    }
//    增加
    @PostMapping("/insert")
    public ResponseData<Boolean> insertFlightInfo(@RequestBody FlightInfo flightInfo){
        Boolean result = flightInfoService.insertFlightInfo( flightInfo );
        if(result){
            return returnBoolean(result, "200", "添加成功");
        }else {
            return returnBoolean(result, "500", "添加失败！该航班号信息已存在");
        }}
//    修改
    @PutMapping("/update")
    public ResponseData<Boolean> updateFlightInfo(@RequestBody FlightInfo flightInfo){
        Boolean updateFlightInfo = flightInfoService.updateFlightInfo( flightInfo);
        return returnBoolean( updateFlightInfo );
    }
}
